package com.tansun.easycare.rule.executionblock.entity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.tansun.rule.utils.StringUtils;


public class BPMNcomm {
	
	private BPMNTask main;//主要组件
	
	private BPMNTask igw;//前网关
	
	private BPMNTask egw;//后网关
	
	private BPMNTask copy;//副本，用于过渡
	
	private BPMNTask flag;//条件是否执行
	
	private BPMNTask end;//条件是否执行
	
	private BPMNTask total;//
	
	private List<SequenceFlow> sflow = new ArrayList<SequenceFlow>();//路线
	
	private String sortLevel;
	
	
	public BPMNcomm(BPMNTask main,String space,String wrap) {
		this.main = main;
		String id = main.getId();
		this.sortLevel = main.getSortLevel();
		String condition = main.getCondition();
		if(StringUtils.isNotEmpty(condition)) {//条件不为空，需要副本，需要前、后网关
       		copy = new BPMNTask();//创建副本
       		copy.setCondition("!("+condition+")");
       		copy.setSortLevel(sortLevel);
       		copy.buildByCode(id+"_copy","", space,wrap);
       		
       		igw = new BPMNTask();//前网关
       		igw.buildGateway(id, "IGW", space, wrap);
       		
       		egw = new BPMNTask();//后网关
       		egw.buildGateway(id, "EGW", space, wrap);
       		
       		SequenceFlow igw2main = new SequenceFlow("IGW"+id,id,condition,"0");//路线,igw-->main-->egw
       		SequenceFlow igw2copy = new SequenceFlow("IGW"+id,id+"_copy","!("+condition+")","1");//路线,igw-->copy-->egw
       		sflow.add(igw2main);
       		sflow.add(igw2copy);
       		
       		if("0".equals(this.sortLevel)) {
	       		SequenceFlow main2egw = new SequenceFlow(id,"EGW"+id,null,"0");
	       		sflow.add(main2egw);
       		}else {
	       		flag = new BPMNTask();
	       		flag.buildByCode("Flag"+StringUtils.generateShortUuid(), "ruleHelper.pkgFlag = false;", space, wrap);
	
	       		SequenceFlow main2flag = new SequenceFlow(id,flag.getId(),null,"0");
	       		SequenceFlow flag2egw = new SequenceFlow(flag.getId(),"EGW"+id,null,"0");
	       		
	       		sflow.add(main2flag);
	       		sflow.add(flag2egw);
       		}
       		SequenceFlow copy2egw = new SequenceFlow(id+"_copy","EGW"+id,null,"1");
       		sflow.add(copy2egw);
		}else {
			if(!"0".equals(this.sortLevel)) {
				flag = new BPMNTask();
	       		flag.buildByCode("Flag"+StringUtils.generateShortUuid(), "ruleHelper.pkgFlag = false;", space, wrap);
	       		SequenceFlow main2flag = new SequenceFlow(id,flag.getId(),null,"0");
	       		sflow.add(main2flag);
			}
		}
	}
	
	public BPMNcomm() {
		super();
	}

	public static List<SequenceFlow> getSequenceFlow(List<BPMNcomm> list) {
		List<SequenceFlow> result = new ArrayList<SequenceFlow>();
		for(int i=0;i<list.size();i++) {
			BPMNcomm bpmncomm = list.get(i);
			List<SequenceFlow> sflow = bpmncomm.getSflow();
			if(sflow.size()==0) {
				if(i==0) {
					
				}else {
					BPMNcomm prev = list.get(i-1);
					SequenceFlow sequenceFlow= prev.connnect(bpmncomm);
					result.add(sequenceFlow);
				}
			}else {
				if(i==0) {
					result.addAll(sflow);
				}else {
					BPMNcomm prev = list.get(i-1);
					SequenceFlow sequenceFlow= prev.connnect(bpmncomm);
					result.add(sequenceFlow);
					result.addAll(sflow);
				}
			}
		}
		return result;
	}

	public static BPMNcomm newFlag(String endCode,String space, String wrap) {
		BPMNTask bpmn = new BPMNTask();
		bpmn.buildByCode("Flag"+StringUtils.generateShortUuid(), "ruleHelper.pkgFlag = true;", space, wrap);
		String id = bpmn.getId();
		BPMNcomm bpmncomm = new BPMNcomm();
		bpmncomm.setMain(bpmn);
		bpmncomm.setSortLevel(bpmn.getSortLevel());
		
		BPMNTask igw = new BPMNTask();//前网关
   		igw.buildGateway(id, "IGW", space, wrap);
   		bpmncomm.setIgw(igw);
   		
   		BPMNTask end = new BPMNTask();
   		end.buildEndEvent("END"+StringUtils.generateShortUuid(), space, wrap);
   		bpmncomm.setEnd(end);
   		
   		BPMNTask total = new BPMNTask();
   		total.buildByCode("Total"+StringUtils.generateShortUuid(), endCode, space, wrap);
   		bpmncomm.setTotal(total);
   		
   		SequenceFlow igw2main = new SequenceFlow("IGW"+id,id,"ruleHelper.pkgFlag == true","1");//如果ruleHelper.pkgFlag == true,不变，表示不命中，即，需要重置ruleHelper.pkgFlag == true
   		SequenceFlow igw2total = new SequenceFlow("IGW"+id,total.getId(),"ruleHelper.pkgFlag == false","0");//如果ruleHelper.pkgFlag == false,改变,表示命中，即，需要结束，
   		SequenceFlow total2end = new SequenceFlow(total.getId(),end.getId(),null,"0");//路线,igw-->copy-->egw
   		
   		bpmncomm.addSequenceFlow(igw2main);
   		bpmncomm.addSequenceFlow(igw2total);
   		bpmncomm.addSequenceFlow(total2end);
		return bpmncomm;
	}
	
	public void addSequenceFlow(SequenceFlow sequenceFlow) {
		this.sflow.add(sequenceFlow);
	}
	public SequenceFlow connnect(BPMNcomm next) {
		if(this.egw == null && next.getIgw() == null) {//指无条件，前一个flag与后一个main
			if(this.flag == null) {
				return new SequenceFlow(this.main.getId(),next.getMain().getId(),null,"0");
			}else {
				return new SequenceFlow(this.flag.getId(),next.getMain().getId(),null,"0");
			}
		}else if(this.egw == null && next.getIgw() != null) {
			if(this.flag == null) {
				return new SequenceFlow(this.main.getId(),"IGW"+next.getMain().getId(),null,"0");
			}else {
				return new SequenceFlow(this.flag.getId(),"IGW"+next.getMain().getId(),null,"0");
			}
		}else if(this.egw != null && next.getIgw() == null) {
			return new SequenceFlow("EGW"+this.main.getId(),next.getMain().getId(),null,"0");
		}else {
			return new SequenceFlow("EGW"+this.main.getId(),"IGW"+next.getMain().getId(),null,"0");
		}
	}
	
	public SequenceFlow connnect(String nextId) {
		if(this.egw != null) {//指无条件，前一个flag与后一个nextId
			return new SequenceFlow("EGW"+this.main.getId(),nextId,null,"0");
		}else {
			if(this.flag == null) {
				return new SequenceFlow(this.main.getId(),nextId,null,"0");
			}else {
				return new SequenceFlow(this.flag.getId(),nextId,null,"0");
			}
		}
	}
	
	public void toBPMNShapeStr(int x,int y,Map<String,BPMNShape> id2BPMNShape) {
		if(igw!=null) id2BPMNShape.put(igw.getId(),igw.toBPMNShape(x, y));
		id2BPMNShape.put(main.getId(),main.toBPMNShape(x+200, y+80));
		if(flag!=null) id2BPMNShape.put(flag.getId(),flag.toBPMNShape(x+400, y+80));
		if(copy!=null) id2BPMNShape.put(copy.getId(),copy.toBPMNShape(x+200, y-80));
		if(egw!=null) id2BPMNShape.put(egw.getId(),egw.toBPMNShape(x+600, y));
		if(total!=null) id2BPMNShape.put(total.getId(),total.toBPMNShape(x+400, y-80));
		if(end!=null) id2BPMNShape.put(end.getId(),end.toBPMNShape(x+200, y-80));
	}
	public String getSortLevel() {
		return sortLevel;
	}

	public void setSortLevel(String sortLevel) {
		this.sortLevel = sortLevel;
	}

	public List<SequenceFlow> getSflow() {
		return sflow;
	}

	public void setSflow(List<SequenceFlow> sflow) {
		this.sflow = sflow;
	}

	public BPMNTask getMain() {
		return main;
	}

	public void setMain(BPMNTask main) {
		this.main = main;
	}

	public BPMNTask getIgw() {
		return igw;
	}

	public void setIgw(BPMNTask igw) {
		this.igw = igw;
	}

	public BPMNTask getEgw() {
		return egw;
	}

	public void setEgw(BPMNTask egw) {
		this.egw = egw;
	}

	public BPMNTask getCopy() {
		return copy;
	}

	public void setCopy(BPMNTask copy) {
		this.copy = copy;
	}

	public BPMNTask getFlag() {
		return flag;
	}

	public void setFlag(BPMNTask flag) {
		this.flag = flag;
	}

	public BPMNTask getEnd() {
		return end;
	}

	public void setEnd(BPMNTask end) {
		this.end = end;
	}

	public BPMNTask getTotal() {
		return total;
	}

	public void setTotal(BPMNTask total) {
		this.total = total;
	}
}
